package logic

import (
	"context"
	"fmt"
	"jd-mall/jd/internal/svc"
	"jd-mall/jd/jd"
	"jd-mall/jd/models"

	"github.com/zeromicro/go-zero/core/logx"
)

type ShopListLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewShopListLogic(ctx context.Context, svcCtx *svc.ServiceContext) *ShopListLogic {
	return &ShopListLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

func (l *ShopListLogic) ShopList(in *jd.ShopListRequest) (*jd.ShopListResponse, error) {
	// todo: add your logic here and delete this line
	page := in.Page
	if page <= 0 {
		page = 1
	}
	pageSize := in.PageSize
	switch {
	case pageSize > 100:
		pageSize = 100
	case pageSize <= 0:
		pageSize = 10
	}
	offset := (page - 1) * pageSize
	var s models.Shop
	models.DB.Debug().Offset(int(offset)).Limit(int(pageSize)).Find(&s)
	var shopList []*jd.ShopList
	for _, m := range shopList {
		list := jd.ShopList{
			Title:     m.Title,
			Price:     m.Price,
			Num:       m.Num,
			Image:     m.Image,
			Status:    1,
			ConStatus: 1,
		}
		shopList = append(shopList, &list)
	}
	fmt.Println(s)
	return &jd.ShopListResponse{
		List: shopList,
	}, nil
}
